<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/7/5
 * Time: 11:37
 */
namespace Mengwu\Controller;

use Admin\Api\MemberApi;
use Mengwu\Api\MwApi;
use Mengwu\Api\YuerjingReplyViewApi;
use Shop\Api\FavoritesApi;
use Think\Model;

class YuerjingController extends BaseStoreController{
    
    /*
     * 妈妈分享
     */
    public function share(){
        $this->assignTitle('妈妈分享');
        $this->assign('yej_list_page');
        $this->queryHotLabels();
        $this->boye_display();
    }

    /**
     * 妈妈提问
     */
    public function question(){
        $this->assignTitle('妈妈提问');
        $this->assign('yej_list_page');
        $this->queryHotLabels();
        $this->boye_display();
    }

    /**
     * 帖子详情
     */
    public function detail($type=false){
        if(!in_array($type,['share','question'])) {
            $this->display_404();
        }

        $this->assign('yej_type',$type);

        $tid = I('tid');
        $p = I('p',1);

        //查询帖子详情
        $data = [
            'tid' => I('tid'),
            'uid' => $this->getUID(),
            'max_reply' => 1,
            'max_comment' => 1,
        ];
        $result = mwApi(MwApi::BY_ForumThread_detail,$data);

        if(empty($result['info'])){
            $this->errorPage('no this thread');
        }

        $detail = $result['info'];
        $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
        $detail['head'] = getHeadUrl($detail['head'],120);
        //处理baby_default时间，性别
        if(!is_null($detail['baby_default'])){

            $baby_default_info = [];
            if($detail['baby_default']['sex']==0){
                $baby_default_info['sex'] = '女宝';
            }
            else if($detail['baby_default']['sex']==1){
                $baby_default_info['sex'] = '男宝';
            }

            $now = date('Y-m-d');
            $birth = date('Y-m-d',$detail['baby_default']['birthday']);

            $jiange = $this->diffDate($now,$birth);
            $baby_default_info['birthday'] = $jiange['year'].'岁'.$jiange['month'].'月'.$jiange['day'].'天';

            $detail['baby_default_info'] = $baby_default_info;

        }

        foreach($detail['img'] as &$val){
            $val = $PICTURE_REMOTE_URL.$val;
        }


        //获取发表时间展示信息
        $time = time() - $detail['dateline'];
        $detail['dateline_show'] = $this->getDateInfo($time,$detail['dateline']);

        $page = ['curpage'=>$p-1, 'size'=>10];
        $result = apiCall(YuerjingReplyViewApi::QUERY, [$tid,$page]);
        if($result['status']){
            $replys = $result['info']['list'];

            //处理头像
            foreach($replys as &$val){
                if(isset($val['author_info'])){
                    $val['author_info']['head'] = getHeadUrl($val['author_info']['head'],120);
                }
            }


            $show = paging($p,$page['size'],$result['info']['count'],'mengwu/yuerjing/detail');
            $this->assign('show',$show);

            $this->assign('replys', $replys);
        }

        $title = !empty($detail['subject']) ? $detail['subject'] : '详情';
        if($type == 'share') $title.= ' 妈妈分享';
        if($type == 'question') $title.= ' 妈妈提问';
        $this->assignTitle($title);

        $this->assign('detail', $detail);


        $this->boye_display();
        
    }

    public function yej_getFavorites(){
        if(IS_POST){
            $this->needLogin();
            $map = [
                'uid' => $this->getUID(),
                'fid' => ['in',[7,8]] //妈妈分享 妈妈提问
            ];
            $page = ['curpage'=>I('p',1),'size'=>10];
            $result = apiCall(FavoritesApi::QUERY_THREAD_WITH_COUNT,[$map,$page]);
            if($result['status']){
                foreach($result['info']['list'] as &$val){
                    if(mb_strlen($val['message'],'utf-8')>190){
                        $val['message'] = mb_substr($val['message'],0,190,'utf-8').'...';
                    }else{
                        $val['message'] = mb_substr($val['message'],0,190,'utf-8');
                    }
                    $this->getUserInfo($val['author_id'],$val);
                }
                $this->ajaxReturnSuc($result['info']);

            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
    }

    /**
     * 取用户昵称
     */
    private function getUserInfo($uid,&$val){
        $result = apiCall(MemberApi::GET_INFO,[['uid'=>$uid]]);
        $val['author_info'] = [];
        if($result['status']){
            if(!is_null($result['info'])){
                $info = $result['info'];
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                $val['author_info']['nickname'] = $info['nickname'];
                $val['author_info']['head'] = getHeadUrl($val['author_info']['head'],120);
            }
        }
    }
    
    private  function getDateInfo($time,$dateline){

        if($time <= 60){
            return "刚刚";
        }else if($time > 60 && $time <= 3600){
            return floor($time/60)."分钟前";
        }else if($time > 3600 && $time <= 3600*24){
            return floor($time/3600)."小时前";
        }else if($time > 3600*24){
            return date('Y-m-d',$dateline);
        }

    }
    private function diffDate($date1,$date2){
        if(strtotime($date1)>strtotime($date2)){
            $tmp=$date2;
            $date2=$date1;
            $date1=$tmp;
        }
        list($Y1,$m1,$d1)=explode('-',$date1);
        list($Y2,$m2,$d2)=explode('-',$date2);
        $Y=$Y2-$Y1;
        $m=$m2-$m1;
        $d=$d2-$d1;
        if($d<0){
            $d+=(int)date('t',strtotime("-1 month $date2"));
            $m--;
        }
        if($m<0){
            $m+=12;
            $Y--;
        }
        return array('year'=>$Y,'month'=>$m,'day'=>$d);
    }

        private function queryHotLabels(){
        //查询热门标签
        $data = [
            'uid' => 0, //暂未使用
            'sort' =>0, //0=>随机,1=>最新,2=>最热
            'number' => I('number',10)  //数量默认10个
        ];

        $result = mwApi(MwApi::BY_Tag_finds,$data);

        $hot_labels = [];
        if($result['status']){
            foreach($result['info'] as $val){
                $hot_labels[] = [
                    'id' => $val['id'],
                    'name'=> $val['name'],
                    'hot' => $val['used'] >=50 ? true : false
                ];
            }
        }
        $this->assign('hot_labels',json_encode($hot_labels));
    }
}